function Init() {
    System.Gadget.onSettingsClosing = SettingsClosing;
    confLoad();
    confInit();

    textboxServer.value = CRM_SERVER;
    textboxOrganization.value = CRM_ORGANIZATION;

    if (CRM_USER == "") {
        checkboxDefaultCredentials.checked = true;
        checkboxDefaultCredentials_changed();
    }
    else {
        textboxUserLogin.value = CRM_USER;
        textboxUserPassword.value = CRM_PASSWORD;
    }

    if (SHOW_TASK) checkboxTask.checked = true; else checkboxTask.checked = false;
    if (SHOW_FAX) checkboxFax.checked = true; else checkboxFax.checked = false;
    if (SHOW_EMAIL) checkboxEmail.checked = true; else checkboxEmail.checked = false;
    if (SHOW_PHONECALL) checkboxPhonecall.checked = true; else checkboxPhonecall.checked = false;
    if (SHOW_APPOINTMENT) checkboxAppointment.checked = true; else checkboxAppointment.checked = false;
    if (SHOW_LETTER) checkboxLetter.checked = true; else checkboxLetter.checked = false;
    if (SHOW_SERVICEAPPOINTMENT) checkboxServiceappointment.checked = true; else checkboxServiceappointment.checked = false;
    if (SHOW_CAMPAIGNRESPONSE) checkboxCampaignresponse.checked = true; else checkboxCampaignresponse.checked = false;
    if (SHOW_CAMPAIGNACTIVITY) checkboxCampaignactivity.checked = true; else checkboxCampaignactivity.checked = false;

    selectorSortby.value = SORTBY;
    selectorTimerInterval.value = TIMER_INTERVAL;
}

function SettingsClosing(event) {
    if (event.closeAction == event.Action.commit) {
        CRM_SERVER = textboxServer.value;
        CRM_ORGANIZATION = textboxOrganization.value;

        if (checkboxDefaultCredentials.checked) {
            CRM_USER = "";
            CRM_PASSWORD = "";
        }
        else {
            CRM_USER = textboxUserLogin.value;
            CRM_PASSWORD = textboxUserPassword.value;
        }

        if (checkboxTask.checked) SHOW_TASK = true; else SHOW_TASK = false;
        if (checkboxFax.checked) SHOW_FAX = true; else SHOW_FAX = false;
        if (checkboxEmail.checked) SHOW_EMAIL = true; else SHOW_EMAIL = false;
        if (checkboxPhonecall.checked) SHOW_PHONECALL = true; else SHOW_PHONECALL = false;
        if (checkboxAppointment.checked) SHOW_APPOINTMENT = true; else SHOW_APPOINTMENT = false;
        if (checkboxLetter.checked) SHOW_LETTER = true; else SHOW_LETTER = false;
        if (checkboxServiceappointment.checked) SHOW_SERVICEAPPOINTMENT = true; else SHOW_SERVICEAPPOINTMENT = false;
        if (checkboxCampaignresponse.checked) SHOW_CAMPAIGNRESPONSE = true; else SHOW_CAMPAIGNRESPONSE = false;
        if (checkboxCampaignactivity.checked) SHOW_CAMPAIGNACTIVITY = true; else SHOW_CAMPAIGNACTIVITY = false;

        SORTBY = selectorSortby.value;
        TIMER_INTERVAL = selectorTimerInterval.value;

        confSave();
    }
    event.cancel = false;
}

function checkboxDefaultCredentials_changed() {
    if (checkboxDefaultCredentials.checked) {
        textboxUserLogin.value = "";
        textboxUserPassword.value = "";
        textboxUserLogin.disabled = true;
        textboxUserPassword.disabled = true;
        textboxUserLogin.style.backgroundColor = "#efefef";
        textboxUserPassword.style.backgroundColor = "#efefef";

    }
    else {
        textboxUserLogin.disabled = false;
        textboxUserPassword.disabled = false;
        textboxUserLogin.style.backgroundColor = "";
        textboxUserPassword.style.backgroundColor = "";
    }
}